﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UAXInversionesWeb.supervisor
{
    public partial class CambiarPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Proxy.isLogged())
            {
                ChangePassword1.UserName = HttpContext.Current.Session["identificador"].ToString();
            }
        }

        protected void ChangePasswordPushButton_Click(object sender, EventArgs e)
        {
            int idcliente = (int)HttpContext.Current.Session["identificador"];

            string oldPass = RijndaelSimple.Encrypt(ChangePassword1.CurrentPassword,
                                            "Pas5pr@se", "s@1tValue", "SHA1",
                                            2, "@1B2c3D4e5F6g7H8", 256);


            string newPass = RijndaelSimple.Encrypt(ChangePassword1.NewPassword,
                                            "Pas5pr@se", "s@1tValue", "SHA1",
                                            2, "@1B2c3D4e5F6g7H8", 256);

            bool resultado = Proxy.cambiarPasswordEmpleado(idcliente, oldPass, newPass);
            if (resultado)
            {
                resetChangePasswordState(ChangePassword1, "success");
            }
            else
            {
                ChangePassword1.ChangePasswordFailureText = "Error al cambiar la contraseña!";
            }
        }

        protected void ContinuePushButton_Click(object sender, EventArgs e)
        {
            resetChangePasswordState(ChangePassword1, "changepassword");
        }

        private void resetChangePasswordState(System.Web.UI.WebControls.ChangePassword cpControl, string state)
        {
            System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic |

            System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static;
            System.Type type = cpControl.GetType();

            System.Reflection.PropertyInfo p = type.GetProperty("CurrentView", flags);
            System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(p.PropertyType);

            object result = converter.ConvertFrom(state);
            p.SetValue(cpControl, result, null);
        }
    }
}
